﻿using Com.Ctrip.Framework.Apollo;
using Microsoft.Extensions.Configuration;

namespace Gateway.Apollo;

/// <summary>
/// apollo 提供器
/// </summary>
public static class ApolloProvider
{
    /// <summary>
    /// 添加apollo 配置
    /// </summary>
    /// <param name="builder"></param>
    public static void AddApolloConfig(this IConfigurationBuilder builder)
    {
        var configuration = builder.Build();

        var namespaces = configuration.GetSection("apolloNameSpace").Get<string[]>();
        var appId = configuration.GetValue<string>("Apollo_AppId");
        var metaServer = configuration.GetValue<string>("Apollo_MetaServer");

        Console.WriteLine("appid is :" + appId);
        Console.WriteLine("metaServer is :" + metaServer);
        var apolloBuilder = builder.AddApollo(appId, metaServer);
        if (namespaces?.Length > 0)
        {
            foreach (var np in namespaces)
            {
                apolloBuilder.AddNamespace(np);
            }
        }
    }
}